
package com.android.formula.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.formula.R;
import com.android.formula.model.NoteSubject;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.task.SetNoteSubjectDBTask;
import com.android.formula.utils.Constants;

public class EditSubjectActivity extends BaseActivity implements TextWatcher {

    private EditText mSubjectNameEdt;

    private Button mSaveBtn;

    private Button mCancelBtn;

    private NoteSubject mNoteSubject;

    @Override
    public void initView() {
        setContentView(R.layout.activity_edit_subject);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        mSubjectNameEdt = (EditText) findViewById(R.id.edit_subject_edt_new_subject);
        mSubjectNameEdt.addTextChangedListener(this);

        mSaveBtn = (Button) findViewById(R.id.edit_subject_btn_save);
        mSaveBtn.setOnClickListener(this);

        mCancelBtn = (Button) findViewById(R.id.edit_subject_btn_cancel);
        mCancelBtn.setOnClickListener(this);
    }

    @Override
    public void loadDataScreen() {
        Bundle bun = getIntent().getExtras();
        mNoteSubject = (NoteSubject) bun.getSerializable(Constants.DATA_KEY);

        if (mNoteSubject != null) {
            mSubjectNameEdt.setText(mNoteSubject.getSubjectName());
        } else {
            finish();
        }
    }

    @Override
    protected void onClickWithDelay(View view) {
        super.onClickWithDelay(view);
        switch (view.getId()) {
            case R.id.edit_subject_btn_save:
                saveSubject();
                break;
            case R.id.edit_subject_btn_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(mSubjectNameEdt.getText().toString().trim())) {
            mSaveBtn.setEnabled(false);
            return;
        } else {
            mSaveBtn.setEnabled(true);
        }
    }

    private void saveSubject() {
        setAutoHideKeyboard(mSubjectNameEdt);
        if (!TextUtils.equals(mNoteSubject.getSubjectName(), mSubjectNameEdt.getText().toString())) {
            mNoteSubject.setSubjectName(mSubjectNameEdt.getText().toString());
            new SetNoteSubjectDBTask(this, mNoteSubject, null, new IDataEventHandler<Integer>() {

                @Override
                public void onNotifyNoData() {
                }

                @Override
                public void onNotifyErrorMessage(String title, String message) {
                }

                @Override
                public void onNotifyData(Integer data) {
                    finish();
                }
            }).execute(mNoteSubject);
        } else {
            finish();
        }
    }

}
